import { useThrottleFn, useSafeState, useCreation } from '.';

import { ThrottleOptions } from './useThrottleFn';

const useThrottle = <T,>(value: T, options?: ThrottleOptions) => {
  const [debounced, setDebounced] = useSafeState(value);

  const run = useThrottleFn(() => {
    setDebounced(value);
  }, options);

  useCreation(() => {
    run();
  }, [value]);

  return debounced;
};

export default useThrottle;
